package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author lvFF
 * @Date 2022/04/26/下午 8:45;
 * Description
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //先保存数据
        this.save(setmealDto);
        //保存第二轮数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //给集合里面的id赋值
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        //保存
        setmealDishService.saveBatch(setmealDishes);
    }
    //试试
    @Override
    public Page<SetmealDto> pageWith(Integer page, Integer pageSize, String name) {
        //
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        //
        QueryWrapper<Setmeal> queryWrapper1 = new QueryWrapper<>();
        //
        queryWrapper1.eq(StringUtils.isNotBlank(name),"name",name);
        //
        queryWrapper1.orderByDesc("update_time");
        //
        this.page(setmealPage,queryWrapper1);
        //
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        //
        List<Setmeal> setmeals = setmealPage.getRecords();
        //
        ArrayList<SetmealDto> list = new ArrayList<>();
        //
        for (Setmeal setmeal : setmeals) {
            SetmealDto setmealDto = new SetmealDto();
            //
            BeanUtils.copyProperties(setmeal,setmealDto);
            //
            Category category = categoryService.getById(setmeal.getCategoryId());
            //
            setmealDto.setCategoryName(category.getName());
            //
            list.add(setmealDto);
        }
        setmealDtoPage.setRecords(list);

        return setmealDtoPage;
    }

    @Override
    public void removeWithDish(Long[] ids) {
        //List<long[]> longs = Arrays.asList(ids);
        //
        QueryWrapper<Setmeal> queryWrapper1 = new QueryWrapper<>();
        //
        queryWrapper1.in("id",Arrays.asList(ids));
        //
        queryWrapper1.eq("status",1);
        //
        int count = this.count(queryWrapper1);
        //
        if (count >0){
            throw  new CustomException("套餐正在售卖，不能删除");
        }
        //
        this.removeByIds(Arrays.asList(ids));

        //2.2 删除关系表setmeal_dish中的数据----SQL：delete from setmeal_dish where setmeal_id in (1,2,3)
        QueryWrapper<SetmealDish> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.in("setmeal_id", Arrays.asList(ids));
        setmealDishService.remove(queryWrapper2);
    }
}
